package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.icCard;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CIcCardsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICIcCardsInfoService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class IcCardAdvancedBu {

    @Autowired
    private ICIcCardsInfoService icCardsInfoService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    private String cardInfStatus;

    //icCard高级操作     cardInfExcuseTare  是否免皮重    cardInfTare 免过皮重(吨)   cardInfPutOuted ic卡是否发放   cardInfReportLost ic卡是否挂失     cardInfCurValid 卡片是否有效
    public SzResult IcCardAdvanced(String id, String cardInfExcuseTareStatus, String cardInfTareNum, String cardInfPutOutedStatus,
                                   String cardInfReportLostStatus, String cardInfCurValidStatus, String token,String plantId) throws Exception {
        QueryWrapper<CIcCardsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("CARDINF_DELETED", 0);
        CIcCardsInfo cIcCardsInfo = icCardsInfoService.getOne(queryWrapper);
        if (cIcCardsInfo == null) {
            return SzResult.error("该卡不存在");
        }

        Integer cardInfCurValid = cIcCardsInfo.getCardinfCurValid();

        if (cardInfReportLostStatus.equals("0")) {
            cIcCardsInfo.setCardinfReportLost(0);//否

            icCardsInfoService.updateById(cIcCardsInfo);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("是否挂失", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否挂失】为【否】", plantId);
        }
        Integer cardinfReportLost1 = cIcCardsInfo.getCardinfReportLost();
        if (cardinfReportLost1 == 1) {
            return SzResult.error("该IC卡已挂失,请先解挂");
        }
        if (cardInfCurValid == 1) {    //ic卡有效,ic卡不能是挂失状态
            Integer cardinfReportLost = cIcCardsInfo.getCardinfReportLost();
            if (cardinfReportLost == 1) {
                return SzResult.error("该IC卡已挂失,请先解挂");
            }
            //IC卡状态不能为注销
            String cardInfStatus = cIcCardsInfo.getCardinfModifyReason();
            if (cardInfStatus.equals("注销")) {
                return SzResult.error("该IC卡已注销,请先启用");
            }
        }
        if (cardInfExcuseTareStatus != null) {
            if (cardInfExcuseTareStatus.equals("1")) {
                cIcCardsInfo.setCardinfExcuseTare(1);//是
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("是否免皮重", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否免皮重】为【是】", plantId);
            } else if (cardInfExcuseTareStatus.equals("0")) {
                cIcCardsInfo.setCardinfExcuseTare(0);//否
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("是否免皮重", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否免皮重】为【否】",  plantId);
            }
        }


        Integer cardInfExcuseTare = cIcCardsInfo.getCardinfExcuseTare();
        BigDecimal cardinfTare = cIcCardsInfo.getCardinfTare();
        //cardinfTare转为String类型
        String cardinfTare1 = cardinfTare.toString();
        //只要cardinfTare1的个位数
        if (cardInfTareNum != null && !cardInfTareNum.equals("")&&!cardInfTareNum.equals(cardinfTare1)) {
            if (cardInfExcuseTare == 1) {
                cIcCardsInfo.setCardinfTare(BigDecimal.valueOf(Long.parseLong(cardInfTareNum)));
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("免过皮重", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的免过皮重为【" + cardInfTareNum + "】吨",  plantId);
            }else {
                return SzResult.error("该IC卡未设置免过皮重");
            }
        }


        if (cardInfPutOutedStatus != null) {
            if (cardInfPutOutedStatus.equals("1")) {
                cIcCardsInfo.setCardinfPutouted(1);//是
                cIcCardsInfo.setCardinfCurValid(1);
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("是否发放", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否发放】为【是】", plantId);
            } else if (cardInfPutOutedStatus.equals("0")) {
                cIcCardsInfo.setCardinfPutouted(0);//否
                cIcCardsInfo.setCardinfCurValid(0);
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("是否发放", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否发放】为【否】",  plantId);
            }
        }

//ic卡是否挂失（1为是，2为否）
        if (cardInfReportLostStatus.equals("1")) {
            cIcCardsInfo.setCardinfReportLost(1);//是
            cIcCardsInfo.setCardinfCurValid(0);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("是否挂失", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否挂失】为【是】", plantId);
        } else if (cardInfReportLostStatus.equals("0")) {
            cIcCardsInfo.setCardinfReportLost(0);//否
            cIcCardsInfo.setCardinfCurValid(1);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("是否挂失", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否挂失】为【否】",  plantId);
        }


        if (cardInfCurValidStatus != null) {
            if (cardInfCurValidStatus.equals("1")) {
                cIcCardsInfo.setCardinfCurValid(1);//是
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("是否有效", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否有效】为【是】", plantId);
            } else if (cardInfCurValidStatus.equals("0")) {
                cIcCardsInfo.setCardinfCurValid(0);//否
                cIcCardsInfo.setDownedID1(0);
                icCardsInfoService.updateById(cIcCardsInfo);
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                sysLogBu.logRecord("是否有效", user.getTrueName(), "修改了卡号为" + cIcCardsInfo.getCardinfCardno() + "的卡的【ic卡是否有效】为【否】",  plantId);
            }


/*            if (cardInfCurValid == 0) {    //ic卡无效,ic卡得是挂失状态
                Integer cardinfReportLost = cIcCardsInfo.getCardinfReportLost();
                if (cardinfReportLost != 1) {
                    return SzResult.error("该IC卡是无效卡");
                }
                //IC卡状态得能为注销
                String cardInfStatus = cIcCardsInfo.getCardinfModifyReason();
                if (cardInfStatus.equals("注销")) {
                    return SzResult.error("该IC卡已注销,请先启用");
                }
            }*/
        }
        return SzResult.Success();
    }
}
